<?php

namespace App\Http\Controllers;
use App\Library\EncryptDecrypt;
use App\Library\Json;
use App\Library\ChargeCache;
use App\Library\Priv;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BaseController extends Controller
{
    use Json;
    use ChargeCache;
    use Priv;

    /**
     * 加密数据
     * @var $Data
     */
    protected $Data;

    /**
     * 设备id
     * @var $OperatorID
     */
    protected $OperatorID;

    /**
     * 请求数据
     * @var array
     */
    protected array $requestData;

    /**
     * 我的方设备号
     * @var string $OperatorID
     */
    protected $myOperatorID = '20228888';


    /**
     * 构造函数
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->OperatorID = $request->post('OperatorID');
        $this->Data = $request->post('Data');
        $check = Validator::make(['OperatorID' => $this->OperatorID, 'Data' => $this->Data], [
            'OperatorID' => 'required',
        ], [
            'OperatorID.required' => '运营商标识'
        ]);
        if ($check->errors()->first()) {
            return $this->jsonData(4003, $check->errors()->first());
        }
        $getIv = $this->getIvData($this->OperatorID);
        $decrypt = EncryptDecrypt::decrypt(json_encode($this->Data), $getIv['DataSecret'], $getIv['DataSecretIV']);
        $this->requestData = json_decode($decrypt, true);
    }

}
